package com.link.base.base.appinfo.controller;

import com.link.base.base.appinfo.model.AppKeyInfo;
import com.link.base.base.appinfo.model.AppLocLog;
import com.link.base.base.appinfo.service.AppLocLogService;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.link.core.basic.service.BasicService.MP;

/**
 * APP日志收集
 *
 * @author ouyangzhenqiang
 * @date 2018/10/16
 */
@Controller
@RequestMapping("/link/appLocLog")
public class AppLocLogController extends BasicController<AppLocLog> {

    @Resource
    private AppLocLogService appLocLogService;

    @Override
    public BasicService<AppLocLog> getBasicService() throws Exception {
        return appLocLogService;
    }

    /**
     * 单条日志记录
     *
     * @author ouyangzhenqiang
     * @date 2018/10/16
     */
    @Override
    @PostMapping(value = "/insert", produces = "application/json", name = "前端单条日志记录")
    @ResponseBody
    public Map<String, Object> insert(@RequestBody AppLocLog record, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);

        try {
            CoreUser user = UserUtil.getUser();
            record.setEmpId(user.getId());
            if (MP.equals(user.getLoginType())) {
                record.setOpenId(user.getOpenId());
            }
            appLocLogService.insert(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.toString());
        }
        return result;
    }

    /**
     * 批量创建日志记录
     *
     * @author ouyangzhenqiang
     * @date 2018/10/16
     */
    @Override
    @PostMapping(value = "/batchInsert", produces = "application/json", name = "前端批量日志记录")
    @ResponseBody
    public Map<String, Object> batchInsert(@RequestBody List<AppLocLog> list,
                                           HttpSession session, HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> result = new HashMap<String, Object>(4);

        try {
            CoreUser user = UserUtil.getUser();
            for (AppLocLog item : list) {
                item.setEmpId(user.getId());
                if (MP.equals(user.getLoginType())) {
                    item.setOpenId(user.getOpenId());
                }
                String info = item.getLocalLogInfo();
                info = new String(Base64.getDecoder().decode(info), StandardCharsets.UTF_8);
                info = URLDecoder.decode(info, "UTF-8");
                item.setLocalLogInfo(info);
                appLocLogService.insert(item);
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.toString());
        }
        return result;
    }

    /**
     * 记录前端定时提交的关键信息
     *
     * @author ouyangzhenqiang
     * @date 2018/10/16
     */
    @PostMapping(value = "/collectKeyInfo", produces = "application/json", name = "前端关键信息日志记录")
    @ResponseBody
    public Map<String, Object> clearMobile(@RequestBody AppKeyInfo record,
                                           HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("collectKeyInfo").withMessage("记录前端定时提交的关键信息...").flush();
        Map<String, Object> result = new HashMap<String, Object>(4);

        try {
            appLocLogService.collectKeyInfo(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.toString());
        }
        return result;
    }
}
